package com.sunzn.media.library.adpt;

import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.sunzn.media.library.R;
import com.sunzn.media.library.bean.Item;
import com.sunzn.media.library.help.MimeType;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class SelectAdapter extends RecyclerView.Adapter<SelectAdapter.ViewHolder> {

    public interface RemoveListener {

        void onRemove(Item item);

        void onSwap(List<Item> data);

    }

    private List<Item> data;

    private RemoveListener listener;

    public class ViewHolder extends RecyclerView.ViewHolder {

        private final AppCompatImageView cover;
        private final AppCompatTextView time;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            itemView.findViewById(R.id.media_select_delete).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onRemove(data.get(getAdapterPosition()));
                    }
                }
            });
            cover = itemView.findViewById(R.id.media_select_cover);
            time = itemView.findViewById(R.id.media_select_time);
        }

        public AppCompatImageView getCover() {
            return cover;
        }

        public AppCompatTextView getTime() {
            return time;
        }

    }

    public SelectAdapter() {
        this.data = new ArrayList<>();
    }

    public void setData(List<Item> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public void RemoveListener(RemoveListener listener) {
        this.listener = listener;
    }

    public void onItemMove(int fromPosition, int toPosition) {
        Collections.swap(data, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
        if (listener != null) {
            listener.onSwap(data);
        }
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.media_select_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Item item = getItem(position);
        Glide.with(holder.getCover()).load(item.getContentUri()).into(holder.getCover());
        holder.getTime().setVisibility(MimeType.isVideo(item.mimeType) ? View.VISIBLE : View.GONE);
        holder.getTime().setText(MimeType.isVideo(item.mimeType) ? DateUtils.formatElapsedTime(item.duration / 1000) : "");
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    public Item getItem(int position) {
        return data.get(position);
    }

}
